package com.augurit.common.sysmeta.convert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.augurit.common.base.extmodel.inner.ExtMetaColumnNode;
import com.augurit.common.entity.SysMetaColumn;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.sysmeta.web.form.SysMetaColumnForm;
import com.augurit.common.system.util.OrgUtils;

public class SysMetaColumnConvertor {
	/**
	 * 将entity转换为form
	 * @param entity
	 * @return
	 */
	public static SysMetaColumnForm convertToForm(SysMetaColumn entity){
		SysMetaColumnForm form = null;
		if(entity != null){
			form = new SysMetaColumnForm();
			form.setId(entity.getId());
			form.setColumnName(entity.getColumnName());
			form.setColumnComments(entity.getColumnComments());
			form.setColumnType(entity.getColumnType());
			form.setColumnLength(entity.getColumnLength());
			form.setColumnPrecision(entity.getColumnPrecision());
			form.setColumnScale(entity.getColumnScale());
			form.setNullable(entity.getNullable());
			form.setIsPrimaryKey(entity.getIsPrimaryKey());
			form.setTableId(entity.getTableId());
			form.setMemo(entity.getMemo());
			form.setPropertyName(entity.getPropertyName());
			form.setFieldId(entity.getFieldId());
			form.setDefaultValue(entity.getDefaultValue());
			form.setDictionary(entity.getDictionary());
			form.setRefColumn(entity.getRefColumn());
			form.setRefTable(entity.getRefTable());
			form.setIsUnique(entity.getIsUnique());
			form.setIsIndex(entity.getIsIndex());
			form.setSqlType(entity.getSqlType());
			form.setIsForeignKey(entity.getIsForeignKey());
		}
		return form;
	}
	
	/**
	 * 
	 * @param form
	 * @param entity
	 */
	public static void  convertToEntity(SysMetaColumnForm form,SysMetaColumn entity){
		if(form != null && entity != null){
			if(form.getId() != null)
				entity.setId(form.getId());
			if(form.getColumnName() != null && form.getColumnName().trim().length()>0)
				entity.setColumnName(form.getColumnName());
			if(form.getColumnComments() != null && form.getColumnComments().trim().length()>0)
				entity.setColumnComments(form.getColumnComments());
			if(form.getColumnLength() != null )
				entity.setColumnLength(form.getColumnLength());
			if(form.getColumnPrecision() != null)
				entity.setColumnPrecision(form.getColumnPrecision());
			if(form.getColumnScale() != null)
				entity.setColumnScale(form.getColumnScale());
			if(form.getNullable()!= null && form.getNullable().trim().length()>0)
				entity.setNullable(form.getNullable());
			if(form.getIsPrimaryKey() != null && form.getIsPrimaryKey().trim().length()>0)
				entity.setIsPrimaryKey(form.getIsPrimaryKey());
			if(form.getColumnType() != null && form.getColumnType().trim().length()>0)
				entity.setColumnType(form.getColumnType().trim());
			if(form.getTableId() != null)
				entity.setTableId(form.getTableId());
			if(form.getMemo() != null && form.getMemo().trim().length()>0)
				entity.setMemo(form.getMemo());
			if(form.getPropertyName() != null && form.getPropertyName().trim().length() > 0)
				entity.setPropertyName(form.getPropertyName());
		}
	}
	
	/**
	 * 将entity列表转换为form列表 
	 * @param list
	 * @return
	 */
	public static List<SysMetaColumnForm> convertEntityListToFormList(List<SysMetaColumn> list,
			List<SysCodeForm> nullableList, 
			List<SysCodeForm> isPrimaryKey){
		List<SysMetaColumnForm> forms = null;
		if(list != null && list.size()>0){
			forms = new ArrayList<SysMetaColumnForm>();
			for(SysMetaColumn entity:list){
				SysMetaColumnForm form = convertToForm(entity);
				
				//扩展属性转换
				form.setNullableLabel(OrgUtils.getItemName(form.getNullable(), nullableList));
				form.setIsPrimaryKeyLabel(OrgUtils.getItemName(form.getIsPrimaryKey(), isPrimaryKey));
				
				forms.add(form);
			}
		}
		return forms;
	}
	/**
	 * 将entity列表转换为form列表 
	 * @param list
	 * @return
	 */
	public static List<SysMetaColumnForm> convertEntityListToFormList(List<SysMetaColumn> list){
		List<SysMetaColumnForm> forms = null;
		if(list != null && list.size()>0){
			forms = new ArrayList<SysMetaColumnForm>();
			for(SysMetaColumn entity:list){
				SysMetaColumnForm form = convertToForm(entity);
				forms.add(form);
			}
		}
		return forms;
	}
	/**
	 * 将form对象列表转换成id列表
	 * @param list
	 * @return
	 */
	public static List<Long> convertFormsToIds(List<SysMetaColumnForm> forms){
		List<Long> ids = null;
		if(forms != null && forms.size()>0){
			ids = new ArrayList<Long>();
			for(SysMetaColumnForm form:forms){
				ids.add(form.getId());
			}
		}
		return ids;
	}
	
	/**
	 * 将form对象转换成Ext节点对象
	 * @param form
	 * @return
	 */
	public static ExtMetaColumnNode convertToExtNode(SysMetaColumnForm form){
		ExtMetaColumnNode node = null;
		if(form != null){
			node = new ExtMetaColumnNode();
			node.setId(ExtMetaColumnNode.TAG+form.getId());
			node.setQtip(form.getColumnComments());
			if(form.getColumnComments() != null && form.getColumnComments().trim().length()>0)
				node.setText(form.getColumnName()+"(" +form.getColumnComments()+")");
			else
				node.setText(form.getColumnName());
			
			node.setValue(form.getPropertyName());
			node.setIconCls(ExtMetaColumnNode.ICON);
			node.setLeaf(true);
		}
		return node;
	}
	
	/**
	 * 将form对象列表转换成Ext节点对象列表
	 * @param forms
	 * @return
	 */
	public static List<ExtMetaColumnNode> convertToExtNodeList(List<SysMetaColumnForm> forms){
		List<ExtMetaColumnNode> nodes = null;
		if(forms != null && forms.size()>0){
			nodes = new ArrayList<ExtMetaColumnNode>();
			for(SysMetaColumnForm form :forms){
				ExtMetaColumnNode node = convertToExtNode(form);
				nodes.add(node);
			}
		}
		return nodes;
	}
}
